//删除商品
function deleteProduct(id){
	if(confirm('您确定要删除此商品？')) {
		window.location.href=jsCtx+"/product/productMessage.do?method=DeleteProduct&id="+id;
	}
}

/**
 * @author BABY
 *修改产品 是否积分
 */
function updateProduct(id,object,sqlKey){
	var array = jQuery("tr#" + id + " td");
	var newValue;
	var json = "{"+object.name+":"+object.value+",id:"+id+"}";
	for(var i=0;i<object.length;i++){
		if(object[i].selected==true){
			newValue=object[i].innerText;
		}
	}
	var Parameter = eval("(" + json + ")");
	productService.updateRecord("product_sqlMap."+sqlKey,Parameter,{callback:function (returnValue) {
		synchroizedSupdate(returnValue, array[7],newValue);
	}});
}
function resultUpdate(value){
	if(value>0){
		alert('修改成功！');
	}
}
/*
*回调函数
*/
function synchroizedSupdate(returnValue, target, value) {
	if (returnValue == 0) {
		return;
	}
	alert('修改成功！');
	target.innerHTML = value;
}
/**
*修改顺序，修改价格
*/
function modifyCP(id,sqlKey,object){
	var array = jQuery("tr#" + id + " td");
	if(sqlKey=='updateSequence'){
		var i=8;
		var value=array[i].innerHTML;
		var newvalue=window.prompt('请输入顺序',value);
		if(newvalue!=null && newvalue!=""){
		
		if(strIsNum(newvalue)){
			if(newvalue>127){
					alert('顺序值只能填写 0~127之间的数字！');
					return;
			}
		}else{
			alert('顺序只能填写数字！');
			return;
		}
		
		var json = "{"+object.name+":"+newvalue+",id:"+id+"}";
		var Parameter = eval("(" + json + ")");
		productService.updateRecord("product_sqlMap."+sqlKey,Parameter,{callback:function (returnValue) {
		synchroizedShow(returnValue, array[i], newvalue);
	}});}
	}else if(sqlKey=='updatePrice'){
		var i=4;//价格
		var isPriceChange=null;
		var value=array[i].innerHTML;
		var newvalue=window.prompt('请输入价格',value);
		var taxPrice=0;
		var Point=0; 
		var newtaxprice=0;
		var newgetpoint=0;
		if(newvalue!=null ){
			if (isNullOrEmpty(newvalue) || isNaN(newvalue) || newvalue >= 1000000 || !checkNumber(5,newvalue)) {
			return;
		}
			taxPrice=getTaxPrice(newvalue);
			Point=getPoint(newvalue);
			newtaxprice=window.prompt('请输入含税价格',taxPrice);
			if (isNullOrEmpty(newtaxprice) || isNaN(newtaxprice) || newtaxprice >= 1000000 || !checkNumber(5,newtaxprice)) {
			return;
		}
			newgetpoint=window.prompt('请输入积分',Point);
			if (isNullOrEmpty(newgetpoint) || isNaN(newgetpoint) || newgetpoint >= 100000 || !checkNumber(5,newgetpoint)) {
			return;
		}
		}else{
			i=5;//含税价格
			taxPrice=array[i].innerHTML;
			newtaxprice=window.prompt('请输入含税价格',taxPrice);
			i=6;//积分
			Point=array[i].innerHTML;
			newgetpoint=window.prompt('请输入积分',Point);
		}
		if(newvalue==null){newvalue=value};
		if(newtaxprice==null){newtaxprice=taxPrice};
		if(newgetpoint==null){newgetpoint=Point};
		var priceName=document.getElementById("priceName").value;
		
		
		var json = "{"+object.name+":"+newvalue+",id:"+id+",taxprice:"+newtaxprice+",getPoint:"+newgetpoint+",pricemodifyPeople:\""+priceName+"\"}";
	
		var Parameter = eval("(" + json + ")");
	
		productService.updateRecord("product_sqlMap."+sqlKey,Parameter,{callback:function (returnValue) {
		synchroizedPriceShow(returnValue,array,newvalue,newtaxprice,newgetpoint);
	}});
	}
}
/*
*价格回调函数
*/
function synchroizedPriceShow(returnValue,array,newvalue,newtaxprice,newgetpoint) {
	if (returnValue == 0) {
		return;
	}
	array[4].innerHTML = newvalue;
	array[5].innerHTML = newtaxprice;
	array[6].innerHTML = newgetpoint;
}
/*
*回调函数
*/
function synchroizedShow(returnValue, target, value) {
	if (returnValue == 0) {
		return;
	}
	target.innerHTML = value;
	
}
/**
按状态查询
*/
function findStatus(Object){
		document.productForm.action=jsCtx+"/product/productMessage.do?method=ProductList";
		document.productForm.submit();
}
/*
*查询可搜索内容
*
function searchDetail(id){
	var catalogId=document.getElementById("product.catalogId");
	if(confirm('注意：改变分类将清空本商品的可搜索内容！')) {
		var json='{id:'+id+'}';
		var Parameter = eval("(" + json + ")");
		productService.updateRecord("product_sqlMap.clearSearchs",Parameter);
		document.subSearchs.action="/Prj4RYNEW/product/productMessage.do?method=getProductSearchs&id="+id+"&catalogId="+catalogId;
		document.subSearchs.submit();
	}else{
		
	}
}
*/
function setSearchs(){
 	
}


function productSelector(typeTabName, type){
	var mode = "list";
	if(isNullOrEmpty(type))
		return;
	if(isNullOrEmpty(typeTabName)){
		mode = "text";
	}
	var arr_ids = document.getElementById(type).value.splitNoNNull(",");
	var valueid=document.getElementById("product.id");
	if(valueid!=null){
		arr_ids.push(valueid.value);
	}
	var result = dialog.showModalDialog(jsCtx+'/product/productMessage.do?method=ProductList&forward=product.productSelector&' + document.getElementById(type).name + "=" + arr_ids.join(",") ,[self, type, mode],'700','460');
	
	if(result == undefined || result.length == 0)
		return;
	var row,tab = document.getElementById(typeTabName);
	for(var i=0;i<result.length;i++){
		row = tab.insertRow();
		row.insertCell();
		row.cells[0].align = "center";
		row.cells[0].className = "td_03";
		row.cells[0].innerHTML = result[i].name;
		row.insertCell();
		row.cells[1].align = "center";
		row.cells[1].className = "td_03";
		row.cells[1].innerHTML = result[i].catalog;
		row.insertCell();
		row.cells[2].align = "center";
		row.cells[2].className = "td_03";
		row.cells[2].innerHTML = result[i].brand;
		row.insertCell();
		row.cells[3].align = "center";
		row.cells[3].className = "td_03";
		row.cells[3].innerHTML = "<a href='javascript:void(0);' onclick='javascript:removeAssociateItem(this,\"" + typeTabName + "\",\"" + type + "\")'>删除</a>"
	}
}

function productResult(){
	var pWindow = window.opener == undefined ? window.dialogArguments[0] : window.opener,mode = window.dialogArguments[2];
	var arr_chk = document.getElementsByName("chkSel");
	var i,ids = new Array(),records = new Array();
	if(arr_chk.length == 0){
		return;
	}
	for(i=0;i<arr_chk.length;i++){
		if(arr_chk[i].checked == true){
			ids.push(arr_chk[i].value);
			if(mode == "text")
				break;
			var arr_td = tree.childs(arr_chk[i].parentNode.parentNode, 1);
			records.push({"name":arr_td[1].innerHTML,"catalog":arr_td[2].innerHTML,"brand":arr_td[3].innerHTML});
		}
	}
	var typeObj = pWindow.document.getElementById(window.dialogArguments[1]);
	if(!isNullOrEmpty(typeObj.value)){
		ids = mode == "text"?ids:typeObj.value.split(",").concat(ids);
	}
	typeObj.value = ids.join(",");
	window.returnValue = records;
	window.close();
}

function removeAssociateItem(self, typeTabName, type){
	var tab = document.getElementById(typeTabName),type = document.getElementById(type),index = self.parentNode.parentNode.rowIndex;
	var ids = type.value.split(",");
	ids.splice(index - 2, 1);
	type.value = ids.join(",");
	tab.deleteRow(index);
}


function removeAssociateItem222222222222(self, typeTabName, type, asfga){
	var tab = document.getElementById(typeTabName),type = document.getElementById(type),index = self.parentNode.parentNode.rowIndex;

	var aaa = document.getElementsByName("product.child")[0].value;
	var a= new Array();
	a = aaa.split(",");
	var ddd = "";
	for(var bbb = 0;bbb<a.length;bbb++){
		var ccc = a[bbb];
		if(ccc==asfga){
					
		}else{
			ddd = ddd + ccc + ",";
		}
	}
	tab.deleteRow(index);
	var sdga = ddd.length-1;
	document.getElementsByName("product.child")[0].value = ddd.substring(0,sdga);
}



function removeAssociateItem11111111(self, typeTabName, type, asfga){

	var tab = document.getElementById(typeTabName),type = document.getElementById(type),index = self.parentNode.parentNode.rowIndex;

	var aaa = document.getElementsByName("product.recommend")[0].value;
	var a= new Array();
	a = aaa.split(",");
	var ddd = "";
	for(var bbb = 0;bbb<a.length;bbb++){
		var ccc = a[bbb];
		if(ccc==asfga){
					
		}else{
			ddd = ddd + ccc + ",";
		}
	}
	tab.deleteRow(index);
	var sdga = ddd.length-1;
	document.getElementsByName("product.recommend")[0].value = ddd.substring(0,sdga);
}

function trim(value){
		return value.replace(/(^\s*)|(\s*$)/g,"");
}
/*
*IN NULL OR EMPTY
*/
function isNullOrEmpty(value){
	if(value != undefined){
		trim(value);
	}
	//value=value.trim();
	var result = false;
	if (value == null || 
		value == undefined || 
		value == "null" || 
		value == ""){
		result = true;
	}
	return result;
}
	
	
// 字符串是否为数字
function strIsNum(str){
	var num;
	
	num = new Number(str);
	return !isNaN(num);
}


//验证填写商品
function checkModifyProduct(){
	//填写可搜索内容
	var inputObject=window.frames["searchs"].document.getElementsByTagName('INPUT');
 	var selectObject=window.frames["searchs"].document.getElementsByTagName('SELECT');
 	for(var i=0;i<selectObject.length;i++){
 		var name=selectObject[i].name;
 		var value=selectObject[i].value;
 		document.getElementById(name).value=value;
 		
 	}
 	for(var n=0;n<inputObject.length;n++){
 		var name=inputObject[n].name;
 		var value=inputObject[n].value;
 		document.getElementById(name).value=value;
 		
 	}	


	var reslut=false;
	var value=document.getElementById("product.pname").value;
	if(isNullOrEmpty(value)){
		alert("请填写商品名称！");
		return reslut;
	}
	
	var value=document.getElementById("checkCodeRe").value;
	if(value=="true"){
		alert("输入的产品编码已存在,请重新输入!");
		return reslut;
	}
	
	value=document.getElementById("product.catalogName").value;
	if(isNullOrEmpty(value)){
		alert("请选择分类");
		return reslut;
	}
	value=document.getElementById("product.code").value;
	if(isNullOrEmpty(value)){
		alert("请填写编码");
		return reslut;
	}
	value=document.getElementById("product.price").value;
	if(isNullOrEmpty(value)){
		alert("请填写商品价格");
		return reslut;
	}else{
		if(strIsNum(value)){
		
		}else{
			alert("价格只能填写数字");
			return reslut;
		}
	}
	value=document.getElementById("product.area").value;
	if(!isNullOrEmpty(value)){
		if(strIsNum(value)){
			alert('请填写正确的产地！');
			return reslut;
		}
	}
	
	value=document.getElementById("product.sequence").value;
	if(isNullOrEmpty(value)){
		alert('请填写顺序！');
		return reslut;
	}else{	
		if(strIsNum(value)){
			if(value>127){
					alert('顺序值只能填写 0~127之间的数字！');
					return reslut;
			}
		}else{
			alert('顺序只能填写数字！');
			return reslut;
		}
	}
	value=document.getElementById("product.clicks").value;
	if(!isNullOrEmpty(value)){
		if(!strIsNum(value)){
			alert('点击量只能填写数字！');
			return reslut;
		}		
	}
	value=document.getElementById("product.present").value;
	if(value.replace(/[^\x00-\xff]/g,"xx").length>16129){
		alert('保修时间太长了！');
		return reslut;
	}
	
	value=document.getElementById("product.description4").value;
	if(value.replace(/[^\x00-\xff]/g,"xx").length>16777215){
		alert('商品简介太长！');
		return reslut;
	}
	
	var oEditor = FCKeditorAPI.GetInstance('product.description1'); 
	var msg = oEditor.GetXHTML( true );
	var msglength=msg.replace(/[^\x00-\xff]/g,"xx").length;
	if(msglength>4294967294){
		alert('商品描述太长,请精简内容！');
		return reslut;
	}
	oEditor=FCKeditorAPI.GetInstance('product.description2'); 
	msg = oEditor.GetXHTML( true );
	msglength=msg.replace(/[^\x00-\xff]/g,"xx").length;
	if(msglength>4294967294){
		alert('商品详细规格太长,请精简内容！');
		
		return reslut;
	}
	oEditor=FCKeditorAPI.GetInstance('product.description3'); 
	msg = oEditor.GetXHTML( true );
	msglength=msg.replace(/[^\x00-\xff]/g,"xx").length;
	if(msglength>4294967294){
		alert('包装清单太长,请精简内容！');
		return reslut;
	}
	oEditor=FCKeditorAPI.GetInstance('product.service'); 
	msg = oEditor.GetXHTML( true );
	msglength=msg.replace(/[^\x00-\xff]/g,"xx").length;
	if(msglength>16777215){
		alert('售后服务内容太长,请精简内容！');
		
		return reslut;
	}
	var methodshen = document.getElementById("method").value;
	if(methodshen == 'addProduct'){
		var tupian1=document.getElementById("product.image1File").value;
		var tupian2=document.getElementById("product.image2File").value;
		var tupian3=document.getElementById("product.image3File").value;
		if(isNullOrEmpty(tupian1)&&isNullOrEmpty(tupian2)&&isNullOrEmpty(tupian3)){
			alert("请添加图片!");
			return reslut;
		}	
	}
	return true;
}


//获取含税价格
function getTaxPriceOrPoint(){
		
var price=document.getElementById("product.price").value;
	if(isNullOrEmpty(price)){
		alert("请填写商品价格");
		return;
	}else{
		if(strIsNum(price)){
		document.getElementById("isPriceChange").value='is';
		var taxPrice=getTaxPrice(price);
		var Point=getPoint(price);
		document.getElementById("product.taxprice").value=taxPrice;		
		document.getElementById("product.getPoint").value=Point;
	}else{
		alert("价格只能填写数字");
		return;
		}
	}
}
/*
	获取积分 价格除以100向下去整
*/
function getPoint(price){
	
	return Math.floor(parseInt(price)/100);
	
}
/*
	5000-9999元的，开票加200元
	10000-19999元的，开票加300元
	20000-49999元的，开票加500元
	50000-99999元的，开票加1000元
	100000元-199999元的，开票加2000元
	200000元-400000元，开票加4000元。
*/
function getTaxPrice(price){
	var price =parseInt(price);
	
	if(5000<=price && price<=9999){
		return parseInt(price)+parseInt(200);
	}else if(10000<=price && price<=19999){
		return parseInt(price)+parseInt(300);
	}else if(20000<=price && price<=49999){
		return parseInt(price)+parseInt(500);
	}else if(50000<=price && price<=99999){
		return parseInt(price)+parseInt(1000);
	}else if(100000<=price && price<=199999){
		return parseInt(price)+parseInt(2000);
	}else if(200000<=price && price<=400000){
		return parseInt(price)+parseInt(4000);
	}else if(price<5000 || price>400000){
		alert('此商品开具手写票（税点统一加4%）');
		return price+price*0.04;
	}
}

function clean(name){
	if(isNullOrEmpty(name))
		return;
	var target = document.getElementById(name);
	if(target == undefined || target == null)
		return;
	target.value = "";
}




